package com.hfengxiang.example.canvasrotatedemo;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class StarDecoration extends RecyclerView.ItemDecoration {
    private int headerHeight = (int) Utils.dpToPixel(45);
    private final Paint headerPaint;
    private final Paint textPaint;
    private final Rect textRect;

    public StarDecoration() {
        headerPaint = new Paint();
        headerPaint.setColor(Color.GREEN);
        textPaint = new Paint();
        textPaint.setTextSize(Utils.dpToPixel(20));
        textPaint.setColor(Color.WHITE);
        textRect = new Rect();
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int childCount = parent.getChildCount();
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            int position = parent.getChildLayoutPosition(child);
            headerPaint.setColor(Color.GREEN);
            // 当item的到达RecyclerView顶部的距离小于headerView的高度与RecyclerView的顶部的padding之和
            // 时不去绘制任何分割线
            if (child.getTop() - headerHeight - parent.getPaddingTop() > 0) {
                if (adapter.isHeaderView(position)) {
                    String group = adapter.getDataSet().get(position).getGroup();
                    textPaint.getTextBounds(group, left, group.length(), textRect);
                    c.drawRect(left, child.getTop() - headerHeight, right, child.getTop(), headerPaint);
                    c.drawText(group, left + Utils.dpToPixel(20), child.getTop() - headerHeight / 2f + textRect.height() / 2f, textPaint);
                } else {
                    c.drawRect(child.getLeft(), child.getTop() - Utils.dpToPixel(1f), child.getRight(),
                            child.getBottom(), headerPaint);
                }
            }
        }
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
        int position = layoutManager.findFirstVisibleItemPosition();
        View itemView = parent.findViewHolderForAdapterPosition(position).itemView;
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        StarAdapter adapter = (StarAdapter) parent.getAdapter();
        String group = adapter.getDataSet().get(position).getGroup();
        textPaint.getTextBounds(group, left, group.length(), textRect);
        headerPaint.setColor(Color.RED);
        int top = parent.getPaddingTop();
        // 当第一个可见的itemView的底部到RecyclerView顶部的距离比headerView的高度小时，吸顶的header的高度
        // 取更小值
        int height = Math.min(headerHeight, itemView.getBottom() - parent.getPaddingTop());
        if (adapter.isHeaderView(position + 1)) {
            c.drawRect(left, top, right,
                    parent.getPaddingTop() + height, headerPaint);
            c.drawText(group, left + Utils.dpToPixel(20), top + height - headerHeight / 2f + textRect.height() / 2f, textPaint);
        } else {
            c.drawRect(left, parent.getPaddingTop(), right,
                    parent.getPaddingTop() + headerHeight, headerPaint);
            c.drawText(group, left + Utils.dpToPixel(20), parent.getPaddingTop() + headerHeight / 2f + textRect.height() / 2f, textPaint);
        }

    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        StarAdapter adapter = (StarAdapter) parent.getAdapter();
        int position = parent.getChildLayoutPosition(view);
        boolean isHeaderView = adapter.isHeaderView(position);
        if (isHeaderView) {
            outRect.set(0, headerHeight, 0, 0);
        } else {
            outRect.set(0, (int) Utils.dpToPixel(1f), 0, 0);
        }

    }
}
